import 'package:flutter/material.dart';
import '../services/deepseek_service.dart';

class AIDiagnosisResultPage extends StatefulWidget {
  final String gender;
  final String age;
  final String bloodType;
  final String currentHistory;
  final String familyHistory;
  final String symptoms;

  const AIDiagnosisResultPage({
    Key? key,
    required this.gender,
    required this.age,
    required this.bloodType,
    required this.currentHistory,
    required this.familyHistory,
    required this.symptoms,
  }) : super(key: key);

  @override
  State<AIDiagnosisResultPage> createState() => _AIDiagnosisResultPageState();
}

class _AIDiagnosisResultPageState extends State<AIDiagnosisResultPage> {
  final List<String> _analysisResults = [];
  bool _isGenerating = true;
  final DeepSeekService _deepSeekService =
      DeepSeekService('sk-4e0e104af45b432d86f0b95db6681b9f');

  @override
  void initState() {
    super.initState();
    _generateDiagnosis();
  }

  Future<void> _generateDiagnosis() async {
    try {
      final prompt = '''
请根据以下患者信息进行专业诊断分析：

性别：${widget.gender}
年龄：${widget.age}
血型：${widget.bloodType}
现病史：${widget.currentHistory}
家族史：${widget.familyHistory}
症状：${widget.symptoms}

请提供以下方面的分析：
1. 潜在疾病
2. 症状分析
3. 诊断依据
4. 建议检查
5. 注意事项

请以专业医生的角度进行分析，并给出详细的建议，请并不要使用markdown语法输出

''';

      final response = await _deepSeekService.getMedicalAdvice(prompt);

      if (mounted) {
        setState(() {
          _analysisResults.addAll(response.split('\n'));
          _isGenerating = false;
        });
      }
    } catch (e) {
      if (mounted) {
        setState(() {
          _analysisResults.add('生成诊断结果时出错：$e');
          _isGenerating = false;
        });
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Stack(
          children: [
            Column(
              children: [
                // 顶部绿色背景区域
                Container(
                  padding: const EdgeInsets.only(
                      left: 20, right: 20, bottom: 20, top: 10),
                  color: const Color(0xFF1DC4A0),
                  child: Row(
                    children: [
                      IconButton(
                        icon: const Icon(Icons.arrow_back, color: Colors.white),
                        onPressed: () => Navigator.pop(context),
                        padding: EdgeInsets.zero,
                      ),
                      const SizedBox(width: 10),
                      const Text(
                        'AI诊断结果',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 24,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ],
                  ),
                ),
                // 分析结果区域
                Expanded(
                  child: SingleChildScrollView(
                    padding: const EdgeInsets.all(20),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        if (_isGenerating)
                          const Center(
                            child: CircularProgressIndicator(),
                          )
                        else
                          ..._analysisResults.map((result) => Padding(
                                padding: const EdgeInsets.only(bottom: 10),
                                child: Text(
                                  result,
                                  style: const TextStyle(fontSize: 16),
                                ),
                              )),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
